/*
 * Copyright (c) 2017-present, CV4J Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.core.filters;

import com.cv4j.core.datamodel.ColorProcessor;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.image.util.Tools;

import java.util.Optional;

/**
 * 运动滤波器
 *
 * @author dev
 * @date 2021/07/22
 */
public class MotionFilter extends BaseFilter {
    private float distance = 10; // default;
    private float onePI = (float) Math.PI;
    private float angle = 0.0f;
    private float zoom = 0.4f;

    /**
     * 得到的距离
     *
     * @return float
     */
    public float getDistance() {
        return distance;
    }

    /**
     * 设置距离
     *
     * @param distance 距离
     */
    public void setDistance(float distance) {
        this.distance = distance;
    }

    /**
     * 得到角
     *
     * @return float
     */
    public float getAngle() {
        return angle;
    }

    /**
     * 组角
     *
     * @param angle 角
     */
    public void setAngle(float angle) {
        this.angle = angle;
    }

    /**
     * 做的过滤器
     *
     * @param src src
     * @return {@link Optional<ImageProcessor>}
     */
    @Override
    public Optional<ImageProcessor> doFilter(ImageProcessor src) {
        int total = width * height;
        byte[][] output = new byte[3][total];
        int cx = width / 2;
        int cy = height / 2;

        // calculate the triangle geometry value
        float sinAngle = (float) Math.sin(angle / 180.0f * onePI);
        float coseAngle = (float) Math.cos(angle / 180.0f * onePI);

        // calculate the distance, same as box blur
        float imageRadius = (float) Math.sqrt(cx * cx + cy * cy);
        float maxDistance = distance + imageRadius * zoom;

        int iteration = (int) maxDistance;
        color(output, sinAngle, coseAngle, iteration);
        ((ColorProcessor) src).putRGB(RED, GREED, BLUE);
        output = null;
        return Optional.ofNullable(src);
    }

    private void color(byte[][] output, float sinAngle, float coseAngle, int iteration) {
        int index = 0;
        int cx = width / 2;
        int cy = height / 2;

        for (int row = 0; row < height; row++) {
            int ta = 0;
            int tr = 0;
            int tg = 0;
            int tb = 0;
            for (int col = 0; col < width; col++) {
                int newX = col;
                int count = 0;
                int newY = row;

                // iterate the source pixels according to distance
                float m11 = 0.0f;
                float m22 = 0.0f;
                for (int i = 0; i < iteration; i++) {
                    newX = col;
                    newY = row;

                    // calculate the operator source pixel
                    if (distance > 0) {
                        newY = (int) Math.floor((newY + i * sinAngle));
                        newX = (int) Math.floor((newX + i * coseAngle));
                    }
                    float f1 = (float) i / iteration;
                    if (newX < 0 || newX >= width) {
                        break;
                    }
                    if (newY < 0 || newY >= height) {
                        break;
                    }

                    // scale the pixels
                    float scale = 1 - zoom * f1;
                    m11 = cx - cx * scale;
                    m22 = cy - cy * scale;
                    newY = (int) (newY * scale + m22);
                    newX = (int) (newX * scale + m11);

                    // blur the pixels, here
                    count++;
                    int idx = newY * width + newX;
                    tr += RED[idx] & 0xff;
                    tg += GREED[idx] & 0xff;
                    tb += BLUE[idx] & 0xff;
                }

                // fill the destination pixel with final RGB value
                if (count == 0) {
                    output[0][index] = RED[index];
                    output[1][index] = GREED[index];
                    output[2][index] = BLUE[index];
                } else {
                    tr = Tools.clamp((int) (tr / count));
                    tg = Tools.clamp((int) (tg / count));
                    tb = Tools.clamp((int) (tb / count));
                    output[0][index] = (byte) tr;
                    output[1][index] = (byte) tg;
                    output[2][index] = (byte) tb;
                }
                index++;
            }
        }
    }

}